import { passLoginRequest, submitSMSLoginRequest } from '@/api/login'
import { getUserInfoRequest } from '@/api/user'
import { setToken, getToken, removeToken, getRefreshToken, setRefreshToken, removeRefreshToken } from '@/utils/auth'
import Cookies from 'js-cookie'
import router from '@/router'
import { Message } from 'element-ui'

export default {
    namespaced: true,
    state: {
        token: getToken() || '',
        refreshToken: getRefreshToken() || '',
        userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},
        menuPermissionList: JSON.parse(localStorage.getItem('menuPermissionList')) || {},
        buttonPermissionList: JSON.parse(localStorage.getItem('buttonPermissionList')) || {},
        flag: false,
        routes: JSON.parse(localStorage.getItem('routes')) || [],
    },
    mutations: {
        // 设置tkoen
        SET_TOKEN(state, payload) {
            state.token = payload.token
            state.refreshToken = payload.refreshToken
            setToken(payload.token)
            setRefreshToken(payload.refreshToken)
        },
        SET_USERINFO(state, payload) {
            localStorage.setItem('userInfo', JSON.stringify(payload))
            state.userInfo = payload
        },
        SET_USERPERMISSION_MENU(state, payload) {
            localStorage.setItem('menuPermissionList', JSON.stringify(payload))
            state.menuPermissionList = payload
        },
        SET_USERPERMISSION_BTN(state, payload) {
            localStorage.setItem('buttonPermissionList', JSON.stringify(payload))
            state.buttonPermissionList = payload
        },
        CHANGE_FLAG(state, payload) {
            state.flag = payload
        },
        SET_ROUTES(state, payload) {
            localStorage.setItem('routes', JSON.stringify(payload))
            state.routes = payload
        }
    },
    actions: {
        //密码登录
        async passwordLogin({ commit }, payload) {
            const res = await passLoginRequest(payload)
            if (res.code !== 200) return Message.error(res.message)
            Cookies.set('userInfo', res.data.userInfo)
            commit('SET_TOKEN', res.data.tokenInfo)
            const userInfo = await getUserInfoRequest()
            if (userInfo.code !== 200) return Message.error(userInfo.message)
            const { menuPermissionList, buttonPermissionList } = res.data.userInfo
            commit('SET_ROUTES', res.data.routeList)
            commit('SET_USERPERMISSION_MENU', menuPermissionList)
            commit('SET_USERPERMISSION_BTN', buttonPermissionList)
            commit('SET_USERINFO', userInfo.data)
            router.push('/')
        },
        //手机号登录
        async phoneLogin({ commit }, payload) {
            const res = await submitSMSLoginRequest(payload)
            if (res.code !== 200) return Message.error(res.message)
            Cookies.set('userInfo', res.data.userInfo)
            commit('SET_ROUTES', res.data.routeList)
            commit('SET_TOKEN', res.data.tokenInfo)
            const userInfo = await getUserInfoRequest()
            if (userInfo.code !== 200) return Message.error(userInfo.message)
            const { menuPermissionList, buttonPermissionList } = res.data.userInfo
            commit('SET_USERPERMISSION_MENU', menuPermissionList)
            commit('SET_USERPERMISSION_BTN', buttonPermissionList)
            commit('SET_USERINFO', userInfo.data)
            router.push('/')
        },
        //微信扫码登录
        async weChatLogin({ commit }, payload) {
            console.log(payload, 'payload');
            commit('SET_TOKEN', payload.tokenInfo)
            const userInfo = await getUserInfoRequest()
            if (userInfo.code !== 200) return Message.error(userInfo.message)
            const { menuPermissionList, buttonPermissionList } = payload.userInfo
            console.log(menuPermissionList, buttonPermissionList, 'buttonPermissionList');
            commit('SET_ROUTES', payload.routeList)
            commit('SET_USERPERMISSION_MENU', menuPermissionList)
            commit('SET_USERPERMISSION_BTN', buttonPermissionList)
            commit('SET_USERINFO', userInfo.data)
            router.push('/')
        }
    },
}
